#!/bin/bash

# BabyTap 应用打包脚本
# 使用方法: ./build-apks.sh [--mobile-only] [--tv-only] [--clean]

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 解析命令行参数
MOBILE_ONLY=false
TV_ONLY=false
CLEAN_BUILD=false

while [[ $# -gt 0 ]]; do
    case $1 in
        --mobile-only)
            MOBILE_ONLY=true
            shift
            ;;
        --tv-only)
            TV_ONLY=true
            shift
            ;;
        --clean)
            CLEAN_BUILD=true
            shift
            ;;
        --help|-h)
            echo "🚀 BabyTap 应用打包脚本"
            echo "========================"
            echo ""
            echo -e "${BLUE}使用方法:${NC}"
            echo "  $0 [选项]"
            echo ""
            echo -e "${BLUE}选项:${NC}"
            echo "  --mobile-only      仅构建 BabyTapMobile APK"
            echo "  --tv-only          仅构建 BabyTapTV APK"
            echo "  --clean            清理构建缓存"
            echo "  --help, -h         显示此帮助信息"
            echo ""
            echo -e "${BLUE}示例:${NC}"
            echo "  $0                构建所有 APK"
            echo "  $0 --mobile-only  仅构建 BabyTapMobile"
            echo "  $0 --tv-only      仅构建 BabyTapTV"
            echo "  $0 --clean        清理构建并重新构建"
            echo ""
            echo -e "${BLUE}输出目录:${NC}"
            echo "  - APK 文件将保存到 ./apk/ 目录"
            echo "  - 目录结构:"
            echo "    ./apk/"
            echo "    ├── BabyTapMobile-release.apk"
            echo "    └── BabyTapTV-release.apk"
            exit 0
            ;;
        *)
            echo -e "${RED}❌ 未知参数: $1${NC}"
            echo "使用方法: $0 [--mobile-only] [--tv-only] [--clean]"
            exit 1
            ;;
    esac
done

echo -e "${BLUE}🚀 BabyTap 应用打包脚本${NC}"
echo "========================"

# 检查是否在正确的目录
if [ ! -d "BabyTapMobile" ] || [ ! -d "BabyTapTV" ]; then
    echo -e "${RED}❌ 错误: 请在项目根目录运行此脚本${NC}"
    echo "确保 BabyTapMobile 和 BabyTapTV 目录存在"
    exit 1
fi

# 创建输出目录
echo -e "${BLUE}📁 创建输出目录...${NC}"
mkdir -p apk

# 构建 BabyTapMobile
build_babytapmobile() {
    echo -e "${BLUE}📱 构建 BabyTapMobile...${NC}"
    echo "================================"
    
    cd BabyTapMobile
    
    # 清理构建缓存（如果需要）
    if [ "$CLEAN_BUILD" = true ]; then
        echo -e "${YELLOW}🧹 清理 BabyTapMobile 构建缓存...${NC}"
        cd android
        ./gradlew clean
        cd ..
        rm -rf node_modules
        npm install
    fi
    
    # 1. 构建 Go 服务器
    echo -e "${BLUE}🔧 步骤 1: 构建 Go 服务器...${NC}"
    if [ ! -d "baby-tap-server" ]; then
        echo -e "${RED}❌ 错误: baby-tap-server 目录不存在${NC}"
        exit 1
    fi

    cd baby-tap-server
    if ! ./build-mobile.sh; then
        echo -e "${RED}❌ Go 服务器构建失败${NC}"
        exit 1
    fi
    cd ..

    echo -e "${GREEN}✅ Go 服务器构建完成${NC}"

    # 2. 安装 Node.js 依赖
    echo -e "${BLUE}📦 步骤 2: 安装 Node.js 依赖...${NC}"
    if ! npm install; then
        echo -e "${RED}❌ Node.js 依赖安装失败${NC}"
        exit 1
    fi

    echo -e "${GREEN}✅ Node.js 依赖安装完成${NC}"

    # 3. 构建 Android APK
    echo -e "${BLUE}📱 步骤 3: 构建 Android APK...${NC}"
    cd android
    if ! ./gradlew assembleRelease; then
        echo -e "${RED}❌ Android APK 构建失败${NC}"
        exit 1
    fi
    cd ..

    echo -e "${GREEN}✅ Android APK 构建完成${NC}"

    # 4. 复制 APK 到输出目录
    echo -e "${BLUE}📋 步骤 4: 复制 APK 文件...${NC}"
    cp android/app/build/outputs/apk/release/app-release.apk ../apk/BabyTapMobile-release.apk

    echo -e "${GREEN}✅ BabyTapMobile 构建完成！${NC}"
    echo -e "${BLUE}📱 APK 文件: ../apk/BabyTapMobile-release.apk${NC}"
    echo -e "${BLUE}📊 文件大小: $(du -h ../apk/BabyTapMobile-release.apk | cut -f1)${NC}"
    
    cd ..
}

# 构建 BabyTapTV
build_babytaptv() {
    echo -e "${BLUE}📺 构建 BabyTapTV...${NC}"
    echo "================================"
    
    cd BabyTapTV
    
    # 清理构建缓存（如果需要）
    if [ "$CLEAN_BUILD" = true ]; then
        echo -e "${YELLOW}🧹 清理 BabyTapTV 构建缓存...${NC}"
        cd android
        ./gradlew clean
        cd ..
        rm -rf node_modules
        npm install
    fi
    
    # 1. 安装 Node.js 依赖
    echo -e "${BLUE}📦 步骤 1: 安装 Node.js 依赖...${NC}"
    if ! npm install; then
        echo -e "${RED}❌ Node.js 依赖安装失败${NC}"
        exit 1
    fi

    echo -e "${GREEN}✅ Node.js 依赖安装完成${NC}"

    # 2. 构建 Android APK
    echo -e "${BLUE}📱 步骤 2: 构建 Android APK...${NC}"
    cd android
    if ! ./gradlew assembleRelease; then
        echo -e "${RED}❌ Android APK 构建失败${NC}"
        exit 1
    fi
    cd ..

    echo -e "${GREEN}✅ Android APK 构建完成${NC}"

    # 3. 复制 APK 到输出目录
    echo -e "${BLUE}📋 步骤 3: 复制 APK 文件...${NC}"
    cp android/app/build/outputs/apk/release/app-release.apk ../apk/BabyTapTV-release.apk

    echo -e "${GREEN}✅ BabyTapTV 构建完成！${NC}"
    echo -e "${BLUE}📺 APK 文件: ../apk/BabyTapTV-release.apk${NC}"
    echo -e "${BLUE}📊 文件大小: $(du -h ../apk/BabyTapTV-release.apk | cut -f1)${NC}"
    
    cd ..
}



# 显示构建结果
show_results() {
    echo ""
    echo -e "${GREEN}🎉 构建完成！${NC}"
    echo "================================"
    echo -e "${BLUE}📁 输出目录: ./apk/${NC}"
    echo ""
    
    if [ -f "apk/BabyTapMobile-release.apk" ]; then
        echo -e "${GREEN}✅ BabyTapMobile-release.apk${NC}"
        echo -e "${BLUE}   大小: $(du -h apk/BabyTapMobile-release.apk | cut -f1)${NC}"
        echo -e "${BLUE}   包名: tech.shupi.babytapmobile${NC}"
    fi
    
    if [ -f "apk/BabyTapTV-release.apk" ]; then
        echo -e "${GREEN}✅ BabyTapTV-release.apk${NC}"
        echo -e "${BLUE}   大小: $(du -h apk/BabyTapTV-release.apk | cut -f1)${NC}"
        echo -e "${BLUE}   包名: tech.shupi.babytaptv${NC}"
    fi
    
    echo ""
    echo -e "${BLUE}💡 安装提示:${NC}"
    echo "  adb install apk/BabyTapMobile-release.apk"
    echo "  adb install apk/BabyTapTV-release.apk"
    echo ""
    echo -e "${BLUE}🔧 其他选项:${NC}"
    echo "  $0 --mobile-only  # 仅构建 BabyTapMobile"
    echo "  $0 --tv-only      # 仅构建 BabyTapTV"
    echo "  $0 --clean        # 清理构建并重新构建"
}

# 主逻辑
if [ "$MOBILE_ONLY" = true ]; then
    echo -e "${BLUE}📱 仅构建 BabyTapMobile 模式${NC}"
    build_babytapmobile
elif [ "$TV_ONLY" = true ]; then
    echo -e "${BLUE}📺 仅构建 BabyTapTV 模式${NC}"
    build_babytaptv
else
    echo -e "${BLUE}🔧 构建所有应用模式${NC}"
    build_babytapmobile
    echo ""
    build_babytaptv
fi

show_results 